package com.best.developer.myarchitecturedemo.data.remote;

import android.app.Application;
import androidx.lifecycle.LiveData;
import androidx.annotation.NonNull;


import com.best.developer.myarchitecturedemo.data.DataSource;
import com.best.developer.myarchitecturedemo.data.local.db.entity.Girl;
import com.best.developer.myarchitecturedemo.data.local.db.entity.ZhihuStory;
import com.best.developer.myarchitecturedemo.data.remote.model.ZhihuStoryDetail;
import com.best.developer.myarchitecturedemo.utils.Util;

import java.util.List;

public class DataRepository {

    private static DataRepository INSTANCE = null;

    private final DataSource mRemoteDataSource;

    private final DataSource mLocalDataSource;

    private static Application sApplication = null;

    private DataRepository(@NonNull DataSource remoteDataSource,
                           @NonNull DataSource localDataSource) {
        mRemoteDataSource = remoteDataSource;
        mLocalDataSource = localDataSource;
    }

    /**
     * 单例模式
     * @param remoteDataSource
     * @param localDataSource
     * @param application
     * @return
     */
    public static DataRepository getInstance(@NonNull DataSource remoteDataSource,
                                      @NonNull DataSource localDataSource,
                                      Application application) {
        if (INSTANCE == null) {
            synchronized (DataRepository.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DataRepository(remoteDataSource, localDataSource);
                    sApplication = application;
                }
            }
        }
        return INSTANCE;
    }

    /**
     * 得到 girl 数据
     * @param index
     * @return
     */
    public LiveData<List<Girl>> getGirlList(int index) {
        if (Util.isNetworkConnected(sApplication.getApplicationContext())) {
            //联网了，就从网络获取
            return mRemoteDataSource.getGirlList(index);
        } else {
            //没有网，就从本地数据库获取
            return mLocalDataSource.getGirlList(index);
        }
    }

    /**
     * 得到是否加载 girl 的布尔值
     * @return
     */
    public LiveData<Boolean> isLoadingGirlList() {
        if (Util.isNetworkConnected(sApplication.getApplicationContext())) {
            return mRemoteDataSource.isLoadingGirlList();
        } else {
            return mLocalDataSource.isLoadingGirlList();
        }
    }


    /**
     * 加载 ZhihuStory 数据
     * @param date
     * @return
     */
    public LiveData<List<ZhihuStory>> getZhihuList(@NonNull String date) {
        if (Util.isNetworkConnected(sApplication.getApplicationContext())) {
            if (date.equals("today")) {   //加载最新的数据
                return mRemoteDataSource.getLastZhihuList();
            } else {   //加载更多数据
                return mRemoteDataSource.getMoreZhihuList(date);
            }
        } else {
            if (date.equals("today")) {
                return mLocalDataSource.getLastZhihuList();
            } else {
                return mLocalDataSource.getMoreZhihuList(date);
            }
        }
    }

    /**
     * 得到特定 id 的 ZhihuStoryDetail 数据
     * @param id
     * @return
     */
    public LiveData<ZhihuStoryDetail> getZhihuDetail(String id) {
        return mRemoteDataSource.getZhihuDetail(id);
    }

    /**
     * 返回是否加载 ZhihuStory 的布尔值
     * @return
     */
    public LiveData<Boolean> isLoadingZhihuList() {
        if (Util.isNetworkConnected(sApplication.getApplicationContext())) {
            return mRemoteDataSource.isLoadingZhihuList();
        } else {
            return mLocalDataSource.isLoadingZhihuList();
        }
    }
}
